FROM php:7.4.30-fpm

ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
	APP_ENV=prod \
    SCAN_CACHEABLE=(true) \
	COMPOSER_CDN=https://mirrors.aliyun.com/composer/ \
    # Setup bento4 environment variables
    PATH=/usr/local/bin/bento4/bin:${PATH}

RUN \
    curl -sfL https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer \
    && chmod +x /usr/bin/composer \
    && composer self-update --clean-backups 2.1.14 \
	&& composer config -g repo.packagist composer ${COMPOSER_CDN} \
    && apt-get update \
    && apt-get install python3 make cmake libstdc++ boost-dev git expect openssh-client openssl tesseract-ocr bash zlib zlib-dev gmp-dev libzip libzip-dev \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS curl-dev openssl-dev pcre-dev pcre2-dev libc-dev libtool freetype-dev libjpeg-turbo-dev libpng-dev \
	
	
	
RUN apt-get update && apt-get upgrade -y \
    && apt-get install -y \
    g++ \
    libbz2-dev \
    libc-client-dev \
    libcurl4-gnutls-dev \
    libedit-dev \
    libfreetype6-dev \
    libicu-dev \
    libjpeg62-turbo-dev \
    libkrb5-dev \
    libldap2-dev \
    libldb-dev \
    libmagickwand-dev \
    libmcrypt-dev \
    libmemcached-dev \
    libpng-dev \
    libpq-dev \
    libsqlite3-dev \
    libssl-dev \
    libreadline-dev \
    libxslt1-dev \
    libzip-dev \
    memcached \
    wget \
    unzip \
    zlib1g-dev \
    && docker-php-ext-install -j$(nproc) \
    bcmath \
    bz2 \
    calendar \
    exif \
    gettext \
    mysqli \
    opcache \
    pdo_mysql \
    pdo_pgsql \
    pgsql \
    soap \
    sockets \
    xmlrpc \
    xsl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && PHP_OPENSSL=yes docker-php-ext-configure imap --with-kerberos --with-imap-ssl \
    && docker-php-ext-install -j$(nproc) imap \
    && docker-php-ext-configure intl \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-configure ldap \
    && docker-php-ext-install ldap \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip \
    && pecl install xdebug && docker-php-ext-enable xdebug \
    && pecl install memcached && docker-php-ext-enable memcached \
    && pecl install mongodb && docker-php-ext-enable mongodb \
    && pecl install redis && docker-php-ext-enable redis \
    && yes '' | pecl install imagick && docker-php-ext-enable imagick \
	
	
	
	
    && docker-php-source delete \
    && apt-get remove -y g++ wget \
    && apt-get autoremove --purge -y && apt-get autoclean -y && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /tmp/* /var/tmp/*
	
	
	
	
	
	# download
	&& git clone https://gitee.com/lujihong/docker.git /tmp/docker \
	&& ls -alh \
	# create /usr/src/php/ext
	&& docker-php-source extract \
    # move files
    && mv /tmp/docker/swoole /usr/src/php/ext/swoole \
    && mv /tmp/docker/yasd /usr/src/php/ext/yasd \
    && mv /tmp/docker/mongodb /usr/src/php/ext/mongodb \
    && mv /tmp/docker/inotify /usr/src/php/ext/inotify \
    && mv /tmp/docker/xlswriter /usr/src/php/ext/xlswriter \

	# install ext
	&& pecl update-channels \
	&& pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-ext-install -j$(nproc) gmp \
    && docker-php-ext-configure zip --with-zip --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install zip \
	&& docker-php-ext-install sockets \
	&& docker-php-ext-install bcmath \
	&& docker-php-ext-install ctype \
	&& docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd \
	&& docker-php-ext-install pdo_mysql \
	&& docker-php-ext-install pcntl \
	&& docker-php-ext-install opcache \
	&& docker-php-ext-configure sysvshm \
		--with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvshm \
	&& docker-php-ext-configure sysvmsg \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvmsg \
	&& docker-php-ext-configure sysvsem \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvsem \

	# swoole
	&& docker-php-ext-configure swoole \
	    --enable-http2   \
	    --enable-mysqlnd \
	    --enable-openssl \
	    --enable-sockets --enable-swoole-curl --enable-swoole-json \
	&& docker-php-ext-install -j$(nproc) swoole \

	# yasd
	&& docker-php-ext-configure yasd \
	&& docker-php-ext-install -j$(nproc) yasd \

	# mongodb
	&& docker-php-ext-configure mongodb \
	    --enable-mongodb \
	&& docker-php-ext-install -j$(nproc) mongodb \

	# inotify
	&& docker-php-ext-configure inotify \
	    --enable-inotify \
	&& docker-php-ext-install inotify \

	# xlswriter
	&& docker-php-ext-configure xlswriter \
	    --enable-reader \
	&& docker-php-ext-install -j$(nproc) xlswriter \

	# Build bento4
	&& cd /tmp/docker/bento4 \
	&& ls -alh \
	&& rm -rf /tmp/docker/bento4/cmakebuild \
	&& mkdir -p /tmp/docker/bento4/cmakebuild/x86_64-unknown-linux \
	&& cd /tmp/docker/bento4/cmakebuild/x86_64-unknown-linux \
	&& cmake -DCMAKE_BUILD_TYPE=Release ../.. \
	&& make \

	# Install bento4
	&& cd /tmp/docker/bento4 && python3 Scripts/SdkPackager.py x86_64-unknown-linux . cmake \
	&& mkdir /usr/local/bin/bento4 \
	&& mv /tmp/docker/bento4/SDK/Bento4-SDK-*.x86_64-unknown-linux/* /usr/local/bin/bento4 \

	#  ---------- some config ----------
	&& echo "opcache.enable_cli = 'On'" > /usr/local/etc/php/conf.d/00_opcache.ini \
	&& echo "swoole.use_shortname = 'Off'" >> /usr/local/etc/php/conf.d/docker-php-ext-swoole.ini \
	&& echo "yasd.debug_mode=cmd" >> /usr/local/etc/php/conf.d/docker-php-ext-yasd.ini \

	# config PHP
	&& cd /usr/local/etc/php \
	&& { \
	    echo "upload_max_filesize=128M"; \
	    echo "post_max_size=128M"; \
	    echo "memory_limit=1G"; \
	    echo "date.timezone=${TIMEZONE}"; \
	} | tee conf.d/99_overrides.ini \

	# config timezone
	&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
	&& echo "${TIMEZONE}" > /etc/timezone \

	# php info
	&& php -v \
	&& php -m \
	&& php --ri swoole \
	&& composer \

	# clear
	&& docker-php-source delete \
	&& apk del .build-deps \
	&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man /usr/src/php.tar.xz* $HOME/.composer/*-old.phar \
	&& echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /usr/share/nginx/html

#COPY . /usr/share/nginx/html

# Composer Cache
# COPY ./composer.* /usr/share/nginx/html/
# RUN composer install --no-dev --no-scripts

#EXPOSE 9000